#include "World.h"

World::World(void)
{
	bullet_manager = BulletManager::getInstance();
	intelligence = new Ia();
	minimap = new Minimap();
	boom = new Explosion();
	crosshair = new Crosshair();
	boom->setPos(Vector3(0,400,0));
}

World::~World(void)
{

}

World* World::getInstance(){
	if (instance == NULL)
		instance = new World();
	return instance;
}

void World::deleteEntity(Entity* ent){
	// if ent == player --> game over
	// else do whatever needed
	toDelete.push_back(ent);
}
/*
void World::init(void)
{
	//Objetos
	tex_manager = TextureManager::getInstance();
	mesh_manager = MeshManager::getInstance();

	spitfire = new Plane();
	spitfire->mesh = mesh_manager->get("spitfire/spitfire.ASE");
	spitfire->texture = tex_manager->get("spitfire/spitfire_color_spec.tga");

	bomber = new EntityMesh();
	bomber->mesh = mesh_manager->get("bombardero/bomber_axis.ASE");
	bomber->texture = tex_manager->get("bombardero/bomber_axis.tga");

	terrain = new EntityMesh();
	terrain->mesh = mesh_manager->get("terrain/terrain.Ase");
	terrain->texture = tex_manager->get("terrain/terrain.tga");

	sky = new EntityMesh();
	sky->mesh = mesh_manager->get("cielo/cielo.ASE");
	sky->texture = tex_manager->get("cielo/cielo.tga");

	controller = new Controller();
	controller->player = spitfire;

	camera = new EntityCamera();
	camera->player = spitfire;
	camera->init();

	//Colocacion inicial objetos
	spitfire->model.setTranslation(0,400,0);
	bomber->model.setTranslation(0,500,200);
	bomber->model.rotateLocal(90*DEG2RAD,Vector3(1,0,0));/// asi keda como para k vuele
}
*/

void World::render(void)
{
	glPushMatrix();
	model.set();
	activeCam->render();
	for(int i = 0; i < children.size(); ++i){
		children[i]->render();
	}
	bullet_manager->render();
	boom->render();
	crosshair->render();
	//minimap->render();
    
	glPopMatrix();


	BulletManager::getInstance()->render();
	player->player->mesh->collision_model->setTransform(player->player->model.m);
	pos->mesh->collision_model->setTransform(pos->model.m);
	int collisions;
	if(collisions = BulletManager::getInstance()->collides(pos)){
		std::cout << collisions << " Bullets collides with terrain" << std::endl;
		std::cout<< BulletManager::getInstance()->bullets.size() << " bullets" << std::endl;
	}
}

void World::update(float dt)
{
	toDelete.clear();
	bullet_manager->update(dt);
	for(int i = 0; i < children.size(); ++i){
		children[i]->update(dt);
	}
	player->player->helice->update(dt); 
	player->update(dt);
	player->mouseMovement();
	boom->update(dt);
	if(enemy->hitpoints>0) intelligence->update(player->player,enemy,dt);
	
	/*
	spitfire->speed=seconds_elapsed * 1000;//the speed is defined by the seconds_elapsed so it goes constant
	controller->update(seconds_elapsed);
	controller->mouseMovement(spitfire->model);	
	camera->update(seconds_elapsed);
	*/
}



World* World::instance = NULL;